<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","group_id");
$FileName       = "ais_tools_groupman.php";
$FileTemplate   = "ais_tools_groupman_form.html";
$SubHeaderTitle = "Group Management";
$TableName		= "tblgroup";
$HTMLFormName   = "UserForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("group_id","group_code","group_name");
buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");

//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
	if (GetParam("Action","")=="Add") {
		FormInsert();
	}
	if (GetParam("Action","")=="Update") {
		$FormMode="Edit";
		FormUpdate();
	}
	if (GetParam("Action","")=="Delete") {
		$FormMode="Edit";
		FormDelete();
	}
	if (GetParam("Action","")=="Cancel") {
		$FormMode="Edit";
		FormCancel();
	}
}

//Field Verification 
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("group_code"=>"Group Code","group_name"=>"Group Name");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";


//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName`";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}


//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

//HTML Component Data

//HTML Value From Database
if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);

//HTML Component Name
$TBS->MergeBlock("bln",$FormCName);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->Delete("$TableName",PRIMARY_KEY);
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield, $ExcludeField="") {
		global $DBConnection;
		$x_field = explode(";",$ExcludeField);
		
		for ($x=0;$x<count($arrayfield);$x++) {
			if (!in_array($arrayfield[$x],$x_field)) 
			   $DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
		}
}


?>